package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.service.ReportService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    private MemberDao memberDao;


    @Autowired
    private OrderDao orderDao;


    @Override
    public Map<String, Object> getBusinessReportData() {
        try {


            // 当前系统时间
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            String reportDate = format.format(new Date());

            // todayNewMember今日新增会员数量
            Integer todayNewMember = memberDao.findMemberCountByToday(reportDate);

            // totalMember总会员数量
            Integer totalMember = memberDao.findTotalMember();

            // thisWeekNewMember 本周新增会员
            // 获取本周周一
            Date date = DateUtils.getThisWeekMonday(new Date());
            String monday = format.format(date);
            Integer thisWeekNewMember = memberDao.findMemberCountByDate(monday);

            // thisMonthNewMember 本月新增会员
            Date firstDay4ThisMonth = DateUtils.getFirstDay4ThisMonth();
            String month = format.format(firstDay4ThisMonth);
            Integer thisMonthNewMember = memberDao.findMemberCountByDate(monday);

            // todayOrderNumber 今日预约人数
            Integer todayOrderNumber = orderDao.findOrderNumberByToday(reportDate);
            //  todayVisitsNumber 今日到诊人数
            Integer todayVisitsNumber = orderDao.findTodayVisitsNumber(reportDate);

            // thisWeekOrderNumber 本周预约人数
            Integer thisWeekOrderNumber = orderDao.findAfterOrderNumber(monday);

            // hisWeekVisitsNumber  本周到诊人数
            Integer thisWeekVisitsNumber = orderDao.findAfterVisitsNumber(monday);

            // thisMonthOrderNumber  本月预约人数
            Integer thisMonthOrderNumber = orderDao.findAfterOrderNumber(month);

            // thisMonthVisitsNumber 本月到诊人数
            Integer thisMonthVisitsNumber = orderDao.findAfterVisitsNumber(month);

            //hotSetmeal :[
            //  {name:'阳光爸妈升级肿瘤12项筛查（男女单人）体检套餐',setmeal_count:200,proportion:0.222},
            // {name:'阳光爸妈升级肿瘤12项筛查体检套餐',setmeal_count:200,proportion:0.222}
            //                    ]
            List<Map> hotSetmeal = orderDao.findHotSetmeal();


            Map<String, Object> data = new HashMap<>();
            data.put("reportDate", reportDate);//当前时间
            data.put("todayNewMember", todayNewMember);//今日注册会员
            data.put("totalMember", totalMember);//总会员注册
            data.put("thisWeekNewMember", thisWeekNewMember);//本周注册会员
            data.put("thisMonthNewMember", thisMonthNewMember);//本月注册毁约
            data.put("todayOrderNumber", todayOrderNumber);//今日预约
            data.put("todayVisitsNumber", todayVisitsNumber);//今日到诊
            data.put("thisWeekOrderNumber", thisWeekOrderNumber);//本周预约
            data.put("thisWeekVisitsNumber", thisWeekVisitsNumber);//本周到诊
            data.put("thisMonthOrderNumber", thisMonthOrderNumber);//本月预约
            data.put("thisMonthVisitsNumber", thisMonthVisitsNumber);//本月到诊
            data.put("hotSetmeal", hotSetmeal);//当前最热套餐

            return data;
        } catch (Exception e) {
            e.printStackTrace();

            throw new RuntimeException(MessageConstant.GET_BUSINESS_REPORT_FAIL);
        }


    }


}
